package com.springbootblog.controller.fontend.thread;

public class RunnableStop implements Runnable
{
    /**
     * 定义线程停止标识
     */
    private boolean flag = true;

    /**
     * 重写run方法
     */
    @Override
    public void run()
    {
        String name = Thread.currentThread().getName();
        Integer i = 0;
        while(this.flag)
        {
            System.out.println(name + " Runnable is running ! " + i++);
            try {
                Thread.sleep(10);
            } catch (
                    InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 停止方法
     */
    public void stop()
    {
        this.flag = false;
    }

    /**
     * 主方法
     */
    public static void main(String[] args)
    {
        // 实例化一个Runnable对象
        RunnableStop runnableStop = new RunnableStop();
        // 开启两个线程
        new Thread(runnableStop,"1").start();
        new Thread(runnableStop,"2").start();

        for (Integer i = 0; i < 100; i++)
        {
            System.out.println("main-for is running! "+ i);

            if(i == 900)
            {
                // 停止线程运行
                runnableStop.stop();
                System.out.println("Runnable is stop！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
            }
        }//*/
    }
}
